package groups;

import lecture.LectureManagedBean;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import lecture.Lecture;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.faces.bean.CustomScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
/**
 *
 * @author Thorsten Ludwig
 */
@ManagedBean(name="exerciseGroupBean")
@CustomScoped(value="#{window}")
public class EditExerciseGroupManagedBean {
    
    @ManagedProperty(value="#{lectureBean}")
    private LectureManagedBean lectureBean;
    
    private String newGroupName;
    
    private List<ExerciseGroupContainer> exerciseGroups;
    
    public EditExerciseGroupManagedBean() {
        System.out.println("Konstruktor von EditExerciseGroupBean aufgerufen");
    }

    public String addGroup() {
        Lecture lecture = this.lectureBean.getLecture();
        ExerciseGroup e = new ExerciseGroup(this.newGroupName,lecture, "");
        lecture.getInfo().addExerciseGroup(e);
        this.exerciseGroups.add(new ExerciseGroupContainer(e));
        return "";
    }
    
    public String removeGroup(ExerciseGroupContainer egb) {
        Lecture lecture = this.lectureBean.getLecture();
        lecture.getInfo().removeExerciseGroup(egb.getEg());
        this.exerciseGroups.remove(egb);
        return "";
    }
    
    public List<ExerciseGroupContainer> getExerciseGroupBeans() {
        if (this.exerciseGroups  == null)
                this.initExerciseGroups();
        return this.exerciseGroups;        
    }
    
    private void initExerciseGroups() {
        List<ExerciseGroupContainer> gb = new ArrayList<ExerciseGroupContainer>();
        Lecture lecture = this.lectureBean.getLecture();
        for (ExerciseGroup eg: lecture.getInfo().getExerciseGroups()) {
            gb.add(new ExerciseGroupContainer(eg));
        }
        System.out.println("Init aufgerufen!");
        this.exerciseGroups = gb;
    }
    
    public String getNewGroupName() {
        return newGroupName;
    }

    public void setNewGroupName(String newGroupName) {
        this.newGroupName = newGroupName;
    }
    
    public LectureManagedBean getLectureBean() {
        return lectureBean;
    }

    public void setLectureBean(LectureManagedBean lectureBean) {
        this.lectureBean = lectureBean;
    }
    
    public List<SelectItem> getDayItems() {
        List<SelectItem> items =  new ArrayList<SelectItem>();
        items.add(new SelectItem(Calendar.MONDAY, "Montag"));
        items.add(new SelectItem(Calendar.TUESDAY, "Dienstag"));
        items.add(new SelectItem(Calendar.WEDNESDAY, "Mittwoch"));
        items.add(new SelectItem(Calendar.THURSDAY, "Donnerstag"));
        items.add(new SelectItem(Calendar.FRIDAY, "Freitag"));
        items.add(new SelectItem(Calendar.SATURDAY, "Samstag"));
        items.add(new SelectItem(Calendar.SUNDAY, "Sonntag"));
        return items;        
    }
    
    public List<SelectItem> getTimeItems() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        List<SelectItem> list = new ArrayList<SelectItem>();
        SelectItem i = new SelectItem();
        i.setLabel("Keine Zeit");
        i.setValue(null);
        list.add(i);
        for (MeetingDate d: lecture.getTimes()) {
            SelectItem item = new SelectItem(d, d.toString());
            list.add(item);
        }
        return list;
    }
}
